﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UniRlv.Utility;

namespace UniRlv.UI.Main.WarerHourseIn
{
    public partial class FrmCriticalError2: Form
    {
        string tmpDir, fileTmp;
        public FrmCriticalError2()
        {
            InitializeComponent();
        }

        private void FrmCriticalError_Load(object sender, EventArgs e)
        {
            FrmMain.GetInstance().CriticalError += CriticalErrorHandler;

            //创建日志文件夹'
            tmpDir = Application.StartupPath + "\\log";
            if (!Directory.Exists(tmpDir))
            {
                Directory.CreateDirectory(tmpDir);
            }

            fileTmp = string.Format("{0}\\{1}_{2}.log", tmpDir, "critical", PublicObject.CurTask.ID);

            if (File.Exists(fileTmp))
            {
                txtLog.Text = File.ReadAllText(fileTmp);
            }
        }

        private void CriticalErrorHandler(object sender, EventArgs e)
        {
            AddLog(FrmMain.GetInstance().GetErrorMsg());
            txtLog.BackColor = Color.Yellow;
        }


        private void btnReset_Click(object sender, EventArgs e)
        {
            txtLog.BackColor = Color.Honeydew;
        }

        void AddLog(string s)
        {
            string msg = string.Format("[{0}]{1}\r\n", DateTime.Now.ToString("HH:mm:ss"), s);
            CallCtrlWithThreadSafety.AppendText(txtLog, msg, this);

            AppendFileLog(msg);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtLog.Text = "";
            File.Delete(fileTmp);
        }

        void AppendFileLog(string s)
        {
            File.AppendAllText(fileTmp, s);
        }
    }
}
